<?php
/**
 * ctl_payment
 *
 * @uses pageFactory
 * @package
 * @version $Id: ctl.payment.php 1867 2008-04-23 04:00:24Z flaboy $
 * @copyright 2003-2007 ShopEx
 * @author Likunpeng <leoleegood@zovatech.com>
 * @license Commercial
 */
include_once('objectPage.php');
class ctl_payment extends objectPage {

    var $workground ='setting';
    var $finder_action_tpl = 'payment/finder_action.html';
    var $object = 'trading/paymentcfg';
    var $editMode = true;
    var $allowExport = false;

    var $disableGridEditCols = "id";
    var $disableColumnEditCols = "id";
    var $disableGridShowCols = "id";
    var $filterUnable = true;
    /**
    * main
    *
    * @access public
    * @return void
    */

    function _detail(){
        return array('show_detail'=>array('label'=>__('支付配置'),'tpl'=>'payment/pay_edit.html'));
    }

    function show_detail($id){
        $oPay = &$this->system->loadModel('trading/payment');
        $aPay = $oPay->getPaymentById($id);
        $this->pagedata['pay'] = $aPay;

        $this->pagedata['pay_info'] = $this->_getPayOpt($aPay['pay_type'], $aPay['custom_name'], $aPay['fee'], $aPay['config'],1);

        $oPlu = $oPay->loadMethod($aPay['pay_type']);
        if($oPlu){
            $this->pagedata['html'] =  $oPlu->infoPad();
        }

        $this->pagedata['pay_id'] = $id;
        $this->pagedata['order'] = $aPay['orderlist'];
        $this->pagedata['old_pay_type'] = $aPay['pay_type'];
        $this->pagedata['pay_des'] = $aPay['des'];
        $this->pagedata['pay_name'] = $aPay['custom_name'];
        $this->pagedata['paylist'] = $oPay->getPluginsArr(true);
    }

    /**
    * main
    *
    * @access public
    * @return void
    */
    function getPayList(){
        $this->path[] = array('text'=>__('支付方式'));
        $oPay = &$this->system->loadModel('trading/payment');
        $this->pagedata['items'] = $oPay->getMethods();
        $this->page('payment/pay_list.html');
    }

    function _getHtmlString($key,&$val,$rs=array(),&$eventScripts){
        unset($selOption);
        unset($tOptions);
        switch($val['type']){
            case 'string':
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'text','name'=>$key,'value'=>$rs[$key]?$rs[$key]:''));
                break;
            case 'select':
                foreach($val['options'] as $k=>$v){
                    $tOptions[$k] = $v;
                    if($rs[$key] == $k){
                        $selOption = $k;
                    }
                }
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'select','name'=>$key,'value'=>$selOption,'options'=>$tOptions));
                break;
            case 'number':
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'text','name'=>$key,'value'=>$rs[$key]?$rs[$key]:''));
                break;
            case 'file':
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'file','name'=>$key,'value'=>$selOption,'options'=>$tOptions));
                break;
            case "radio":
                foreach($val['options'] as $k => $v){
                    $checked="";
                    if ($rs[$key]==$k)
                        $checked="checked";
                    $tOptions[$k]=$v;
                    if ($rs[$key]==$k)
                        $selOption=$k;
                }
               
                if ($val['extendcontent']){
                    unset($extendContent);
                    foreach($val['extendcontent'] as $ck => $cv){
                        $scripts.="<script>";
                        if (isset($rs[$key]))
                           if ($rs[$key])
                               $scripts.="$('".$cv['property']['extconId']."').show();";
                           else
                               $scripts.="$('".$cv['property']['extconId']."').hide();";
                        else{
                            if ($cv['property']['display'])
                                $scripts.="$('".$cv['property']['extconId']."').show();";
                            else
                                $scripts.="$('".$cv['property']['extconId']."').hide();";
                        } 
                        $scripts.="</script>";
                        $i=0;
                        $type=$cv['property']['type'];
                        $name=$cv['property']['name'];
                        $size=$cv['property']['size']?$cv['property']['size']:4;
                        unset($extendContent);
                        foreach($cv['value'] as $csk => $csv){ 
                            unset($checked);
                            if (!$rs)
                                $checked='checked=true';
                            if (in_array($csv['value'],$rs[$name]))
                                $checked='checked=true';
                            $csv['imgname'] = $csv['imgname']?"<img src=".$this->system->base_url()."plugins/payment/images/".$csv['imgname'].">":$csv['name'];
                            $val['extendcontent'][$ck]['value'][$csk]['imgname']=$csv['imgname'];
                            $val['extendcontent'][$ck]['value'][$csk]['checked']=$checked;
                        }
                    }
                    
                }
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'radio','name'=>$key,'value'=>$selOption,'options'=>$tOptions),'extendContent'=>$val['extendcontent']);
                if ($val['event']){
                    $aTemp['params']['onclick']=$val['event'].'(this);';
                }
                if ($val['eventscripts']){
                    $eventScripts=$val['eventscripts'].$scripts;
                }
                break;
            default:
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'text','name'=>$key,'value'=>$rs[$key]?$rs[$key]:''));
                break;
        }
        return $aTemp;
    }

    /**
    * savePayment
    *
    * @access public
    * @return void
    */
    function savePayment(){
        if($_POST['pay_id']){
            $this->begin('index.php?ctl=trading/payment&act=detail&p[0]='.$_POST['pay_id']);
        }else{
            $this->begin('index.php?ctl=trading/payment&act=index');
        }
        $oPay = &$this->system->loadModel('trading/payment');
        if ($_POST['paymethod']==1)
            $_POST['fee'] = $_POST['fee'] / 100;
        if ($_FILES){//是否有文件上传
            $file=&$this->system->loadModel("system/sfile");
            foreach($_FILES as $key => $val){
                if (intval($val['size'])>0){
                    $_POST[$key]=$val['name'];
                    switch ($_POST['pay_type']){
                        case "ICBC"://工商银行
                            if ($key=="keyFile"){//商户私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="key"){
                                    trigger_error(__('商户私钥文件格式有误,请上传key格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            elseif ($key == "certFile"||$key =="icbcFile"){//商户公钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="crt"){
                                    if($key=="certFile")
                                        trigger_error(__('商户公钥文件格式有误,请上传crt格式文件'),E_USER_ERROR);
                                    else
                                        trigger_error(__('工行公钥文件格式有误,请上传crt格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        case "HYL"://广东好易联
                            if ($key == "keyFile"){//私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="pem"){
                                    trigger_error(__('私钥文件格式有误,请上传key格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            elseif ($key == "certFile"){//公钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="cer"){
                                    trigger_error(__('公钥文件格式有误,请上传cer格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        default:
                            break;

                    }
                    $file->UploadPaymentFile($val,$_POST['pay_type']);//上传支付相关文件
                }
            }
        }
        $this->end($oPay->updatePay($_POST), __('保存成功！'));
    }

    /**
    * addpayment
    *
    * @access public
    * @return void
    */
    function addPayment(){
        $this->begin('index.php?ctl=trading/payment&act=index');
        $oPay = &$this->system->loadModel('trading/payment');
        if ($_POST['paymethod']==1)
            $_POST['fee'] = $_POST['fee'] / 100;
        if ($_FILES){//是否有文件上传
            $file=&$this->system->loadModel("system/sfile");
            foreach($_FILES as $key => $val){
                if (intval($val['size'])>0){
                    $_POST[$key]=$val['name'];
                    switch ($_POST['pay_type']){
                        case "ICBC"://工商银行
                            if ($key=="keyFile"){//商户私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="key"){
                                    trigger_error(__('文件格式有误,请上传key格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            elseif ($key == "certFile"||$key =="icbcFile"){//商户公钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="crt"){
                                    trigger_error(__('文件格式有误,请上传crt格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        case "HYL"://广东好易联
                            if ($key == "keyFile"){//私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="pem"){
                                    trigger_error(__('文件格式有误,请上传pem格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            elseif ($key == "certFile"){//公钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="cer"){
                                    trigger_error(__('文件格式有误,请上传cer格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        case "skypay":
                            if ($key=="keyFile" || $key =="certFile"){//私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="key"){
                                    trigger_error(__('文件格式有误,请上传key格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        default:
                            break;

                    }
                    $file->UploadPaymentFile($val,$_POST['pay_type']);//上传支付相关文件
                }
            }
        }
        $this->end($oPay->insertPay($_POST,$msg),$msg);
    }

    /**
    * addpayment
    *
    * @access public
    * @return void
    */
    function delPayment($sId){
        $this->begin('index.php?ctl=trading/payment&act=index');
        $oPay = &$this->system->loadModel('trading/payment');
        $this->end($oPay->deletePay($sId),__('删除成功！'));
    }
    /**
    * newPayment
    *
    * @access public
    * @return void
    */
    function newPayment(){
        $this->path[] = array('text'=>__('添加支付方式'));
        $oPay = &$this->system->loadModel('trading/payment');
        $this->pagedata['paylist'] = $oPay->getPluginsArr(true);
        $this->page('payment/pay_new.html');
    }

    /**
    * detailPayment
    *
    * @access public
    * @return void
    */
    function detailPayment($id){
        $this->path[] = array('text'=>__('支付方式配置'));
        $oPay = &$this->system->loadModel('trading/payment');
        //$oPay->getPluginsArr(true);
        $aPay = $oPay->getPaymentById($id);
        $this->pagedata['pay'] = $aPay;
        $this->pagedata['pay_info'] = $this->_getPayOpt($aPay['pay_type'], $aPay['custom_name'], $aPay['fee'], $aPay['config']);
        $this->pagedata['pay_id'] = $id;
        $this->pagedata['order'] = $aPay['orderlist'];
        $this->pagedata['old_pay_type'] = $aPay['pay_type'];
        $this->pagedata['pay_des'] = $aPay['des'];
        $this->pagedata['pay_name'] = $aPay['custom_name'];
        $this->pagedata['paylist'] = $oPay->getPluginsArr(true);
        $this->page('payment/pay_edit.html');
    }

    /**
    * getPayOpt
    *
    * @access public
    * @return void
    */
    function getPayOpt($sType, $sPayName=''){
        header('Content-Type: text/html;charset=utf-8');
        if(!$sType){
            echo ' ';
        }else{
            echo $this->_getPayOpt($sType, $sPayName);
            $this->loadJs();
        }
    }

    function _getPayOpt($sType, $sPayName='', $nFee='', $config='',$fetch=0){
        $oPay = &$this->system->loadModel('trading/payment');
        $oPlu = $oPay->loadMethod($sType);
        if($aThisPayCur = $oPay->getSupportCur($oPlu)){
            if($aThisPayCur['DEFAULT']){
               
                $curName = __('商店默认货币');
            }else{
                $oCur = &$this->system->loadModel('system/cur');
                $aCurLang = $oCur->getSysCur();
                if($aThisPayCur['ALL']){
                    $aThisPayCur = $aCurLang;
                }
                foreach($aThisPayCur as $k=>$v){
                    $curName .= $aCurLang[$k].",&nbsp;";
                    $curName=$curName?rtrim($curName,',&nbsp;'):'';
                }
            }
        }
        if($oPlu){
            $aTemp = unserialize($config);
            if($aTemp){
                foreach($aTemp as $key=>$val){
                    if ($key<>'method'&&$key<>'fee')
                     $aPay[$key]=$val;
                }
            }
            $aField = $oPlu->getfields();
            foreach($aField as $key=>$val){
                $PayPlugItem[] = $this->_getHtmlString($key,$val,$aPay,$eventScripts);
            }
        }
        if ($aTemp['method']==1||!isset($aTemp['method']))
            $check1='checked';
        elseif ($aTemp['method']==2)
            $check2='checked';
        $this->pagedata['sPayName'] = $sPayName;
        $this->pagedata['curName']  = $curName;
        $this->pagedata['PayPlugItem'] = $PayPlugItem;
        $this->pagedata['fee'] = array($nFee,$aTemp['fee']);
        $this->pagedata['checked']=array($check1,$check2);
        $this->pagedata['eventScripts'] = $eventScripts;
        $this->pagedata['hiddenmethod'] = $aTemp['method']; 
        if ($fetch)
            return $this->fetch('payment/pay_detail.html');
        else
            $this->display('payment/pay_detail.html');
    }

    function infoPad($pid){
        header('Content-Type: text/html;charset=utf-8');
        if(!$pid){
            echo ' ';
        }else{
            $oPay = &$this->system->loadModel('trading/payment');
            $oPlu = $oPay->loadMethod($pid);
            $infoPad = '';
            if($oPlu){
                $infoPad = $oPlu->infoPad();
            }
            echo $infoPad;
        }
    }
    function loadJs(){
        echo "<script>";
        echo "var ipt=$('setfee').getElements('input[type=radio]').addEvent('click',function(){";
        echo "    setDisable(this.value);";
        echo "});";
        echo "var setDisable=function(v){";
        echo "if (v==2){";
        echo "      $('fix').show().getElements('input[type=text]').set('disabled',false);";
        echo "      $('rate').hide().getElements('input[type=text]').set('disabled',true);";
        echo "}else{";
        echo "      $('rate').show().getElements('input[type=text]').set('disabled',false);";
        echo "      $('fix').hide().getElements('input[type=text]').set('disabled',true);";
        echo "}";
        echo "};";
        echo "var mtd=$('hiddenmethod').get('text');";
        echo "setDisable(mtd);";
        echo "</script>";
    }
}

?>
